﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System;

public class MemDiffWnd : EditorWindow
{
    GroupMemSnapshot gms1 = null;
    GroupMemSnapshot gms2 = null;
    string snapfile1 = "";
    string snapfile2 = "";
    string snapfileName1 = "";
    string snapfileName2 = "";

    [MenuItem("Window/MemDiff")]
    static void show()
    {
        MemDiffWnd wnd = EditorWindow.GetWindow<MemDiffWnd>();
        wnd.Show();
    }

    MemDiffWnd()
    {
        titleContent = new GUIContent("MemDiffWnd");
    }

    bool m_bFull = false;
    bool bFull
    {
        get { return m_bFull; }
        set
        {
            m_bFull = value;
			m_bOutline = value ? false : true;
        }
    }

    bool m_bOutline = true;
	bool bOutline
	{
		get { return m_bOutline; }
		set
		{ 
			m_bOutline = value;
			m_bFull = value ? false : true;
		}
	}

    int m_classSizeFilter = 0;
	int classSizeFilter
	{
		get { return m_classSizeFilter; }
		set 
		{
			m_classSizeFilter = 0 < value ? value : 0;
		}
	}

	int m_itemSizeFilter = 0;
	int itemSizeFilter
	{
		get { return m_itemSizeFilter; }
		set 
		{
			m_itemSizeFilter = 0 < value ? value : 0;
		}
	}

    bool bNative = true;
    bool bManage = false;
    string classFilter = "";

    private void OnGUI()
    {
		if (null == gms1)
			snapfileName1 = "";
		if (null == gms2)
			snapfileName2 = "";

        GUILayout.Space(10);
		
        GUILayout.BeginHorizontal();
        GUILayout.TextField(snapfileName1);
        if (GUILayout.Button("load snapshot1"))
        {
			snapfile1 = EditorUtility.OpenFilePanelWithFilters("select snapshot file", "", new string[] { "memsnap", "memsnap3" });
            if (string.IsNullOrEmpty(snapfile1))
            {
                gms1 = null;
                snapfileName1 = "";
            }
            else
            {
                gms1 = new GroupMemSnapshot(snapfile1);
                snapfileName1 = Path.GetFileNameWithoutExtension(snapfile1);
				GC.Collect ();
            }
        }
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
		GUILayout.TextField(snapfileName2);
        if (GUILayout.Button("load snapshot2"))
        {
            snapfile2 = EditorUtility.OpenFilePanelWithFilters("select snapshot file", "", new string[] { "memsnap", "memsnap3" });
            if (string.IsNullOrEmpty(snapfile2))
            {
                gms2 = null;
                snapfileName2 = "";
            }
            else
            {
                gms2 = new GroupMemSnapshot(snapfile2);
                snapfileName2 = Path.GetFileNameWithoutExtension(snapfile2);
				GC.Collect ();
            }
		}
		GUILayout.EndHorizontal();

        GUILayout.Space(5);
        GUILayout.Label("数据粒度：");
        GUILayout.BeginHorizontal("helpbox");
        bOutline = GUILayout.Toggle(bOutline, "outline", EditorStyles.radioButton);
        bFull = GUILayout.Toggle(bFull, "full", EditorStyles.radioButton);
        GUILayout.EndHorizontal();


        EditorGUI.BeginDisabledGroup(bOutline);
        GUILayout.Space(5);
        GUILayout.BeginVertical("helpbox");
        GUILayout.Label("数据类型：");
        GUILayout.BeginHorizontal();
        bNative = GUILayout.Toggle(bNative, "native", EditorStyles.toggle);
        bManage = GUILayout.Toggle(bManage, "manage", EditorStyles.toggle);
        GUILayout.EndHorizontal();

        GUILayout.Space(5);
        GUILayout.Label("类型(过滤，比如Texture2D，为空则比较所有类型):");
        classFilter = GUILayout.TextField(classFilter);

        GUILayout.Space(5);
		itemSizeFilter = EditorGUILayout.IntField ("item >= (kb)", itemSizeFilter);
        GUILayout.EndVertical();
        EditorGUI.EndDisabledGroup();

        GUILayout.Space(5);
		classSizeFilter = EditorGUILayout.IntField ("class >= (kb)", classSizeFilter);

		if (GUILayout.Button ("diff")) 
		{
			if (null == gms1 || null == gms2) 
			{
				EditorUtility.DisplayDialog ("error", "diff failed, mem snap1 or mem snap2 is null!", "ok");
			} 
			else 
			{
				if (bFull)
					gms1.Diff (gms2, m_classSizeFilter * 1024, m_itemSizeFilter*1024, classFilter, bNative, bManage);
				else
					gms1.Diff (gms2, m_classSizeFilter * 1024);
				GC.Collect ();
			}
		}
    }
}
